using System;
using System.Collections;
using System.Reflection;
using Copperline.Escriba;

namespace Copperline.Escriba.Config
{
	/// <summary>
	/// Summary description for LoggerFactory.
	/// </summary>
	public class LoggerFactory
	{
		private LoggerFactory()
		{
		}

		static public void Create( ArrayList aLoggers )
		{
			foreach( UserLogger ul in aLoggers )
			{
				string type;
				if( ul.Type.StartsWith( ConfigHelper.NAMESPACE ) )
					type = ul.Type;

				else
					type = ConfigHelper.NAMESPACE + "." + ul.Type;

				ILogger logger = Activator.CreateInstance( Type.GetType( type ) ) as ILogger;
				Logger.AddLogger( ul.Name, logger );

				PropertyInfo[] aProps = logger.GetType().GetProperties();
				foreach( PropertyInfo prop in aProps )
				{
					try
					{
						foreach( string key in ul.Properties.Keys )
						{
							NameValuePair nvp = ul.Properties[key] as NameValuePair;
							if( nvp.Name.Equals( prop.Name ) )
							{
								prop.SetValue( logger, nvp.Value, null );
								break;
							}
						}
					}
					catch( Exception )
					{}
				}
			}
		}
	}
}
